package cn.xhh.dailyreview.server.model;

import java.util.Calendar;
import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.apache.commons.lang.StringUtils;

import cn.xhh.dailyreview.server.utils.Utility;

import com.google.appengine.api.datastore.Text;

/**
 * The model class: Review<br/>
 * Relations:<br/>
 * User-to-Review: one-to-many,<br/>
 * Category-to-Review: one-to-many.
 * @author cooky
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Review {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;
	
	@Persistent
	private Text content;
	
	@Persistent
	private Date creationTime;
	
	@Persistent
	private Date lastModified;
	
	@Persistent
	private Long categoryId;
	
	@Persistent
	private Long userId;
	
	public Review(Category category, Text content) {
		this.content = content;
		this.categoryId = category.getId();
		this.userId = category.getUserId();
		this.creationTime = this.lastModified = new Date();
	}

	public Long getId() {
		return id;
	}
	
	public Text getContent() {
		return content;
	}

	public void setContent(Text content) {
		this.content = content;
	}

	public Date getCreationTime() {
		return creationTime;
	}

	public void setCreationTime(Date creationTime) {
		this.creationTime = creationTime;
	}

	public Date getLastModified() {
		return lastModified;
	}

	public void setLastModified(Date lastModified) {
		this.lastModified = lastModified;
	}

	public Long getCategoryId() {
		return categoryId;
	}

	public void setCategory(Category category) {
		if (category != null) {
			this.categoryId = category.getId();
			this.userId = category.getUserId();
		}
	}

	public Long getUserId() {
		return userId;
	}

	public void setUesr(Long userId) {
		this.userId = userId;
	}
	
	/**
	 * Detail of the review in format "[creationTime] content", e.g. <br/>
	 * [2009-09-20] Today I've done a rather bad thing...
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		String dateStr = creationTime==null ? null : Utility.getDateFormat().format(creationTime);
		String contentStr = content==null ? null : content.getValue();
		if (contentStr!=null && contentStr.length()>100)
			contentStr = contentStr.substring(0, 100) + " ....";
		return "[" + dateStr + "] " + contentStr;
	}

	public boolean atDate(Date d) {
		if (d == null) return false;
		
		Calendar curr = Calendar.getInstance();
		curr.setTime(creationTime);
		Calendar compare = Calendar.getInstance();
		compare.setTime(d);
		
		return curr.get(Calendar.YEAR) == compare.get(Calendar.YEAR)
			&& curr.get(Calendar.DAY_OF_YEAR) == compare.get(Calendar.DAY_OF_YEAR);
	}
	
	public boolean valid() {
		return userId != null && userId > 0
			&& categoryId != null && categoryId > 0
			&& content != null && StringUtils.isNotBlank(content.getValue())
			&& creationTime != null
			&& lastModified != null;
	}
	
	public boolean invalid() {
		return !valid();
	}
	
}
